package com.gonsin.gview.common.utils;

import java.io.File;

public class PathUtils {


    public static String getPathFromUrl(String tempPath, String url){
        if(url == null){
            return null;
        }
        String tempUrl = "/temp";
        if(isWindows()) {
            tempPath = tempPath.replace("\\", "\\\\");
        }
        if(url.startsWith(tempUrl)){
            url = url.replaceFirst(tempUrl, tempPath);
            url = url.replace("/", File.separator);
            return url;
        }

        return null;
    }

    private static boolean isWindows(){
        // 使用Java类System可以获取到大多数系统信息
        String os = System.getProperty("os.name");
        if(os.toLowerCase().startsWith("win")){
            return true;
        }
        return false;
    }

    public static String getUrlFromPath(String tempPath, String path) {
        if(path == null){
            return null;
        }
//        String tempPath = getTempPath().replace("\\", File.separator);
//        String filePath = getFilePath().replace("\\", File.separator);
        path = path.replace(File.separator, "/");
        path = path.replace(":", "");
        if(path.startsWith(tempPath)){
            path = path.replaceFirst(tempPath, "/temp");
            path = path.replace(File.separator, "/");
            return path;
        }

        return path;

    }
}
